package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.Coupon;
import com.tbit.uqbike.object.pojo.FestivalActivity;
import com.tbit.uqbike.service.business.FestivalActivityService;
import com.tbit.uqbike.webmanager.dao.core.CouponDao;
import com.tbit.uqbike.webmanager.dao.core.FestivalActivityDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 节日赠送配置
 * @Date: 创建于 17:48 2020/6/9
 */
@Service
public class FestivalActivityServiceImpl implements FestivalActivityService {
    @Autowired
    private FestivalActivityDao festivalActivityDao;
    @Autowired
    private CouponDao couponDao;

    @Override
    public List<FestivalActivity> queryPage(Integer accountId, Integer pageNO, Integer rowCount) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        map.put("pageNO", (pageNO - 1) * rowCount);
        map.put("rowCount", rowCount);
        return festivalActivityDao.queryPage(map);
    }

    @Override
    public Integer queryCount(Integer accountId) {
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", accountId);
        return festivalActivityDao.queryCount(map);
    }

    @Override
    public void insert(FestivalActivity festivalActivity) {
        if (festivalActivity.getCouponId() != null) {
            Coupon coupon = couponDao.getByCouponId(festivalActivity.getCouponId());
            if (coupon == null) {
                throw new BaseException("coupon_not_exit");
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("accountId", festivalActivity.getAccountId());
        map.put("festivalType", festivalActivity.getFestivalType());
        FestivalActivity festivalActivityDB = festivalActivityDao.getByAccountIdAndType(map);
        if (festivalActivityDB != null) {
            throw new BaseException("ver.festivalType.is.exist");
        }
        festivalActivityDao.insert(festivalActivity);
    }

    @Override
    public void del(Integer festivalactivityId) {
        festivalActivityDao.del(festivalactivityId);
    }
}
